package com.audi.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Patterns;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.audi.R;

public class SignupActivity extends Activity {

    private Context mContext;
    private EditText edtFirstName, edtLastName, edtEmail, edtPassword,
	    edtRetypePassword;
    private Button btnSignup;

    protected void onCreate(Bundle bundle) {
	super.onCreate(bundle);
	this.requestWindowFeature(Window.FEATURE_NO_TITLE);
	setContentView(R.layout.activity_signup);

	initView();
	initData();
	initListener();
    }

    private void initView() {
	edtFirstName = (EditText) findViewById(R.id.edt_first_name);
	edtLastName = (EditText) findViewById(R.id.edt_last_name);
	edtEmail = (EditText) findViewById(R.id.edt_email);
	edtPassword = (EditText) findViewById(R.id.edt_password);
	edtRetypePassword = (EditText) findViewById(R.id.edt_retype_password);
	btnSignup = (Button) findViewById(R.id.btn_signup);
    }

    private void initData() {
	mContext = SignupActivity.this;
    }

    private void initListener() {
	btnSignup.setOnClickListener(new OnClickListener() {

	    @Override
	    public void onClick(View view) {
		signUp();
	    }
	});
    }

    private void signUp() {
	if (TextUtils.isEmpty(edtFirstName.getText().toString().trim())) {
	    Toast.makeText(mContext, "Please input first name",
		    Toast.LENGTH_SHORT).show();
	    return;
	}
	if (TextUtils.isEmpty(edtLastName.getText().toString().trim())) {
	    Toast.makeText(mContext, "Please input last name",
		    Toast.LENGTH_SHORT).show();
	    return;
	}
	if (TextUtils.isEmpty(edtEmail.getText().toString().trim())) {
	    Toast.makeText(mContext, "Please input email", Toast.LENGTH_SHORT)
		    .show();
	    return;
	}
	if (!Patterns.EMAIL_ADDRESS.matcher(
		edtEmail.getText().toString().trim()).matches()) {
	    Toast.makeText(mContext, "Email invalid", Toast.LENGTH_SHORT)
		    .show();
	    return;
	}
	if (TextUtils.isEmpty(edtPassword.getText().toString().trim())) {
	    Toast.makeText(mContext, "Please input password",
		    Toast.LENGTH_SHORT).show();
	    return;
	}
	if (edtPassword.getText().toString().trim().length() < 8
		|| edtPassword.getText().toString().trim().length() > 25) {
	    Toast.makeText(
		    mContext,
		    "The length of password should be between 8 to 25 characters.",
		    Toast.LENGTH_SHORT).show();
	    return;
	}

	if (!edtPassword.getText().toString().trim().matches("[A-Za-z0-9]+")) {
	    Toast.makeText(mContext,
		    "Password contain characters (A-Z), (a-z), (0-9)",
		    Toast.LENGTH_SHORT).show();
	    return;
	}

	if (!edtPassword.getText().toString().trim()
		.equals(edtRetypePassword.getText().toString().trim())) {
	    Toast.makeText(mContext,
		    "In correct password. Please confirm your password again.",
		    Toast.LENGTH_SHORT).show();
	    return;
	}
//	startActivity(new Intent(mContext, TutorialActivity.class));
    }

}